package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.SetMealService;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetMealMapper setMealMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //判断是否已经存在数据
        //新建一个购物车对象
        ShoppingCart shoppingCart = new ShoppingCart();
        //拷贝属性到购物车对象
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //获取线程中的用户id
        Long userId = BaseContext.getCurrentId();
        //设置用户id
        shoppingCart.setUserId(userId);
        //查询数据库中的数据
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //如果存在，数量加1，
        if(list != null && list.size()>0){
            //获取到购物车中的商品数量
            ShoppingCart cart = list.get(0);
            //更改购物车中的商品数量
            cart.setNumber(cart.getNumber()+1);
            //更新数据库商品数量信息
            shoppingCartMapper.updateNumberById(cart);
        }else{
            // 不存在，添加到购物车，数量默认为1
           Long dishId = shoppingCartDTO.getDishId();
           if(dishId != null){
               //添加的是菜品
               Dish dish =dishMapper.getById(dishId);
               shoppingCart.setName(dish.getName());
               shoppingCart.setImage(dish.getImage());
               shoppingCart.setAmount(dish.getPrice());

           }else{
               //添加的是套餐
               //获取套餐id
               Long setmealId = shoppingCartDTO.getSetmealId();
               //根据id查询套餐信息
               SetmealVO setmeal = setMealMapper.getById(setmealId);
               //设置套餐名称和图片
               shoppingCart.setName(setmeal.getName());
               shoppingCart.setImage(setmeal.getImage());
               shoppingCart.setAmount(setmeal.getPrice());
           }
            //公共字段赋值
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //插入到数据库中
            shoppingCartMapper.insert(shoppingCart);

        }


    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        ShoppingCart shoppingCart = new ShoppingCart();
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list =shoppingCartMapper.list(shoppingCart);
        return list;
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteAll(userId);
    }

    /**
     * 删除购物车中的一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(userId)
                .build();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //查询数据库
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if(list != null && list.size()>0){
            //获取到购物车中的商品数量
            ShoppingCart cart = list.get(0);
            Integer number = cart.getNumber();
            if(number == 1){
                //当前商品只有一份  直接删除
                shoppingCartMapper.deleteById(cart.getId());
            }else{
                //当前商品不止一份  数量减一
                cart.setNumber(cart.getNumber()-1);
                shoppingCartMapper.updateNumberById(cart);
            }
        }

    }
}
